package org.ejde.sciencelogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 * This class contains the attributes and behaviors for the
 * local event log monitor window.  The montior window
 * displays the messages submitted by the local user.
 * 
 * @author Eric Martin
 * @version 0.1
 */
public class smackChatWindow extends JFrame implements WindowListener,
		PacketListener {

	/**
	 * The JPanel object for the log monitor
	 */
	private JPanel dialogPanel;

	/**
	 * The JScrollPane object for the log monitor
	 */
	private JScrollPane dialogScrollPane;

	/**
	 * The JTextPane object for the log monitor
	 */
	private JTextPane editorPane;

	/**
	 * The StyledDocument object for the log monitor
	 */
	private StyledDocument doc;
	
	/**
	 * The Style object for the log monitor
	 */
	private Style def;

	/**
	 * Constructor method.  Create the event log monitor
	 * JFrame.
	 * 
	 * @throws HeadlessException
	 */
	public smackChatWindow() {

		super("Recently Logged");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		// Create the JComponent objects
		this.dialogPanel = new JPanel(new BorderLayout());
		this.editorPane = new JTextPane();
		this.editorPane.setEditable(false);
		this.dialogScrollPane = new JScrollPane(editorPane);

		// Initialize/Configure Style objects
		this.initStyles();

		// Set the scroll pane behaviors
		this.dialogScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		// Add the dialog Scroll Pane to the dialog panel.  
		this.dialogPanel.add("Center", dialogScrollPane);

		// Setup the Containing JFrame
		this.setContentPane(this.dialogPanel);
		this.setSize(800, 400);
		this.setVisible(true);

	}

	/**
	 * Process the incoming XMPP message (packet).
	 * 
	 * @param pack the XMPP message to process.
	 */
	public void processPacket(Packet pack) {
		
		Message msg = (Message) pack;
		
		// Build the message to display
		// First add the JID that submitted the event.
		String from = getPacketAuthor(msg);
		
		// Second add a time stamp to show when the message
		// arrived.
		SimpleDateFormat timefmt = new SimpleDateFormat("HH:mm:ss");
		Date now = new Date();
		String when = "("	+ timefmt.format(now) + "): ";
		
		// Lastly add the contents of the event.
		String event = msg.getBody();
		
		// Add tabs at the beginning of each new line after
		// the first line.
		event.replaceAll("\n", "\n\t\t");

		// Format the message and send it to the editor
		// pane.
		try {
			
			this.doc.insertString(doc.getLength(), from, this.doc.getStyle("bold"));
			this.doc.insertString(doc.getLength(), when, this.doc.getStyle("bold"));
			this.doc.insertString(doc.getLength(), event + "\n", this.doc.getStyle("small"));
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
		// Move the cursor to the bottom of the editor
		// pane.
		this.editorPane.setCaretPosition(this.doc.getLength());

	}

	/**
	 * Retrieve the XMPP username  
	 * @param msg XMPP message.
	 * @return the username of the event author as a
	 * string.
	 */
	public String getPacketAuthor(Message msg) {
		String[] username = msg.getFrom().split("/");
		return username[1];
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosed(WindowEvent e) {
		this.setVisible(false);

	}

	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Initalize the text style attributes of the editor pane
	 */
	private void initStyles() {

		this.doc = editorPane.getStyledDocument();
		this.def = StyleContext.getDefaultStyleContext().getStyle(
				StyleContext.DEFAULT_STYLE);
		
		Style regular = this.doc.addStyle("regular", this.def);
		StyleConstants.setFontFamily(def, "SansSerif");

		Style s = this.doc.addStyle("italic", regular);

		StyleConstants.setItalic(s, true);

		s = this.doc.addStyle("bold", regular);
		StyleConstants.setBold(s, true);
		StyleConstants.setForeground(s, Color.RED);

		s = this.doc.addStyle("small", regular);
		StyleConstants.setFontSize(s, 10);

		s = this.doc.addStyle("large", regular);
		StyleConstants.setFontSize(s, 16);

	}

}
